<?php

/**
 *    资金管理控制器
 *
 *    @author    censhuixing
 *    @time    2014-05-12
 */
class PaycenterApp extends MemberbaseApp
{

	function index()
	{
		$this->account_info();
	}
	//资金管理首页
	function account_info()
	{

		// 当前位置
		$this->_capital_position('账户信息');
		/* 当前用户中心菜单 */
		$this->_curitem('my_account_information');

		/* 当前所处子菜单 */
		$this->_curmenu('my_account_information');

		$user = $this->visitor->get();
		//用户资金
		$model_usermoney =& m('usermoney');
		$usermoney_info  = $model_usermoney->get_info_by_user_id($user['user_id']);
		$usermoney_info['user_money']  = number_format($usermoney_info['user_money'],2);
		$usermoney_info['frozen_money'] = number_format($usermoney_info['frozen_money'],2);

		//安全等级
		$i = 0;
		$user_safety_mod = &m('usersafety');
		$accout_security = $user_safety_mod->get(array('conditions'    => 'user_id = ' . $user['user_id']));
		if($accout_security['pay_password']){$i++;}
		if($accout_security['binding_email']){$i++;}
		if($accout_security['binding_mobile']){$i++;}

		$safe = array('0'=>'低','1'=>'低','2'=>'中','3'=>'高',);

		$this->assign('user',$user);
		$this->assign('usermoney_info',$usermoney_info);
		$this->assign('safe_level',$safe[$i]);

		$this->import_resource(array(
		'script' => array(
		array(
		'path' => 'dialog/dialog.js',
		'attr' => 'id="dialog_js"',
		),
		array(
		'path' => 'jquery.ui/jquery.ui.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.ui/i18n/' . i18n_code() . '.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.plugins/jquery.validate.js',
		'attr' => '',
		),
		),
		'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css',
		));

		$this->display('account_info.html');

	}

	//网上充值
	function recharge()
	{

		// 当前位置
		$this->_capital_position('网上充值');

		/* 当前用户中心菜单 */
		$this->_curitem('my_recharge');

		/* 当前所处子菜单 */
		$this->_curmenu('my_recharge');
		//表单没有提交
		$this->import_resource(array(
		'script' => array(

		array(
		'path' => 'jquery.plugins/jquery.validate.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.plugins/additional-methods.js',
		'attr' => '',
		),
		),

		));
		//返回修改时用
		$money  =  empty($_GET['money'])?'':floatval($_GET['money']);
		$pay_bank  =  empty($_GET['pay_bank'])?'':html_script(trim($_GET['pay_bank']));
		$pay_type  =  empty($_GET['pay_type'])?'0':html_script(trim($_GET['pay_type']));

		$this->assign('money',$money);
		$this->assign('pay_bank', $pay_bank);
		$this->assign('pay_type', $pay_type);
		$this->assign('user_name', $this->visitor->get('user_name'));

		$this->display('capital_recharge.html');


	}
	//提交表单
	function recharge_submit()
	{
		$money  = floatval($_POST['money']);//充值金额
		if($money <= 0)
		{
			$this->show_warning('充值金额必须大于0元');
			exit;
		}
		$pay_type  = intval(trim($_POST['pay_type']));//充值方式
		$pay_bank  = html_script(trim($_POST['pay_bank']));//选择了哪个银行
		$params=base64_encode($money.','.$pay_type.','.$pay_bank);
		//重新跳转，防止表单刷新
		header('Location:index.php?app=paycenter&act=pay_confirm&from='.$_GET['from'].'&params='.$params);
	}

	//兑换礼品卡
	function recharge_card()
	{
		/*$user_safety_mod = &m('usersafety');
		$accout_security = $user_safety_mod->get("user_id =".$this->visitor->get('user_id'));
		if(empty($accout_security['pay_password']))
		{
		$this->show_warning('为了您的账号安全,请先设置支付密码再兑换礼品卡','立刻设置支付密码','index.php?app=account_security&act=authen_verify&mode=set_pay_password');
		exit;
		}*/
		// 当前位置
		$this->_capital_position('激活礼品卡');

		/* 当前用户中心菜单 */
		$this->_curitem('my_recharge_card');

		/* 当前所处子菜单 */
		$this->_curmenu('my_recharge_card');

		//兑换成功后的页面
		if($_GET['succeed']==1)
		{

			$card_number  = html_script(trim($_GET['card']));//哪个卡号

			if(empty($card_number))
			{
				$this->show_warning('非法操作！');
				exit;
			}

			$pointcard_mod =& m('pointcard');
			$card_info = $pointcard_mod->get(array(
			'fields' => "denomination,use_time",
			'conditions' => "card_sn='$card_number'",
			));

			if(empty($card_info))
			{
				$this->show_warning('该卡号不存在！');
				exit;
			}

			$model_usermoney =& m('usermoney');
			$accout_info = $model_usermoney->get("user_id =".$this->visitor->get('user_id'));//查看有没有资金记录
			$this->assign('accout_info', $accout_info);
			$this->assign('card_info', $card_info);
			$this->display('capital_recharge_card_succeed.html');
		}
		else
		{

			$this->import_resource(array(
			'script' => array(
			array(
			'path' => 'jquery.plugins/jquery.validate.js',
			'attr' => '',
			),
			array(
			'path' => 'jquery.plugins/additional-methods.js',
			'attr' => '',
			),
			),

			));

			$this->display('capital_recharge_card.html');
		}
	}

	//提交表单兑换礼品卡
	function recharge_card_submit()
	{
		$msg_data=array('done'=>false,'msg'=>'');
		$card_sn  = html_script(trim($_POST['card_number']));
		$password  = html_script(trim($_POST['card_psw']));
		$password  =  strtoupper($password);
		if(!$card_sn || !$password)
		{
			$msg_data['msg'] = '卡号与密码都不能为空';
			echo json_encode($msg_data);
			exit;
		}
		$pointcard_mod =& m('pointcard');
		$cards = $pointcard_mod->get("card_sn = '".$card_sn."'");
		if(!$cards)
		{
			$msg_data['msg'] = '卡号不正确';
			echo json_encode($msg_data);
			exit;
		}
		if($cards['password'] !=md5($password))
		{
			$msg_data['msg'] = '密码不正确';
			echo json_encode($msg_data);
			exit;
		}
		if($cards['use_status'] ==1)
		{
			$msg_data['msg'] = '该卡已经使用,不能重复使用';
			echo json_encode($msg_data);
			exit;
		}
		if($cards['is_valid'] ==0)
		{
			$msg_data['msg'] = '该卡还没有激活';
			echo json_encode($msg_data);
			exit;
		}
		if($cards['is_valid'] ==2)
		{
			$msg_data['msg'] = '该卡已经作废';
			echo json_encode($msg_data);
			exit;
		}
		if($cards['valid_time'] < gmtime())
		{
			$msg_data['msg'] = '该卡已经过期,不能再使用';
			echo json_encode($msg_data);
			exit;
		}

		$pointcard_mod->edit($cards['card_id'], array('use_status'=>1,'use_time'=>gmtime()));//更新礼品卡使用状态与使用时间
		if ($pointcard_mod->has_error())
		{
			$msg_data['msg'] = $pointcard_mod->get_error();
			echo json_encode($msg_data);
			exit;
		}
		$model_usermoney =& m('usermoney');
		$accout_info = $model_usermoney->get("user_id =".$this->visitor->get('user_id'));//查看有没有资金记录
		if($accout_info)
		{
			$model_usermoney->edit($accout_info['money_id'], array('user_money'=>$accout_info['user_money']+$cards['denomination']));
			if ($model_usermoney->has_error())
			{
				$msg_data['msg'] = $model_usermoney->get_error();
				echo json_encode($msg_data);
				exit;
			}
		}
		else
		{
			$accout_in = array(
			'user_id' => $this->visitor->get('user_id'),
			'user_money' => $cards['denomination'],
			);
			if (!$model_usermoney->add($accout_in))
			{
				$msg_data['msg'] = $model_usermoney->get_error();
				echo json_encode($msg_data);
				exit;
			}

		}

		$accout_log = array(
		'user_id' => $this->visitor->get('user_id'),
		'order_no' => $cards['card_sn'],
		'user_money' => $cards['denomination'],
		'change_time' => gmtime(),
		'change_desc' => '激活礼品卡',
		'change_type' =>3,
		);

		$model_accountlog =& m('accountlog');//添加到账户充值记录
		if (!$model_accountlog->add($accout_log))
		{
			$msg_data['msg'] = $model_accountlog->get_error();
			echo json_encode($msg_data);
			exit;
		}
		$msg_data['done'] = true;
		$msg_data['msg'] = '成功激活礼品卡';
		echo json_encode($msg_data);
		exit;

	}

	//提交表单
	function pay_confirm()
	{

		//表单提交后确认页
		$this->_capital_position('网上充值');

		/* 当前用户中心菜单 */
		$this->_curitem('my_recharge');

		/* 当前所处子菜单 */
		$this->_curmenu('my_recharge');
		//表单提交
		$params = html_script(trim($_GET['params']));
		$arr     =  explode(',',base64_decode($params));
		$money   =  $arr[0];
		$pay_type  =  $arr[1];
		$pay_bank  =  $arr[2];
		$user_name = $this->visitor->get('user_name');

		$this->assign('money',$money);
		$this->assign('pay_type',$pay_type);//充值方式
		$this->assign('pay_bank', $pay_bank);//选择了哪个银行，同时显示页的显示的支付方式图片
		$this->assign('user_name', $user_name);

		$time  = time();
		$key   = '~!4c26e267d63fcd9abd19e0c8920989';

		$auth=base64_encode($money.','.$pay_type.','.$pay_bank.','.$time);
		$url= "auth=".$auth."&sign=".md5($time.$key);

		$this->assign('url',$url);

		$this->display('capital_recharge_confirm.html');

	}

	//选择什么支付方式
	function pay_to_channel()
	{
		$auth    =  html_script($_GET['auth']);
		$arr     =  explode(',',base64_decode($auth));
		$money   =  $arr[0];
		$pay_type  =  $arr[1];//支付方式
		$pay_bank  =  $arr[2];
		$time  =  $arr[3];
		$sign    =  html_script($_GET['sign']);

		if($time<time()-300)  $this->show_warning('超时了，请重试');

		if(empty($money) || empty($pay_bank) || empty($time) || empty($sign))
		$this->show_warning('参数错误！');

		$API_SECURITY_TICKET = '~!4c26e267d63fcd9abd19e0c8920989';
		$ticket= md5($time.$API_SECURITY_TICKET);
		if($sign != $ticket) $this->show_warning('效验码错误');

		if($pay_type == 1)
		$payment = 'alipay_bank';//支付宝网上银行
		if($pay_type == 2)
		$payment = 'alipay';//支付宝

		//验证通过，则交易入库处理
		$order = array();
		$order = array(
		'trans_uid'     => $this->visitor->get('user_id'),
		'trans_code'    => $this->genOrderCode(),
		'trans_amount'  => $money,
		'trans_intime' => time(),
		'trans_payment' => $payment,
		'trans_ip'      => real_ip(),
		);
		$model_trans =& m('user_trans');
		if (!($trans_id = $model_trans->add($order)))//入库
		{
			$this->pop_warning('服务器繁忙，请稍后重试！');
		}
		else  //调用支付方式，跳转到支付网关
		{
			$code = $order['trans_payment'];

			include_once(ROOT_PATH."/includes/payments/{$code}.php");

			$pay = new $code();

			$order['pay_bank']   = $pay_bank;
			$order['body']       = '账户充值';
			$order['notify_url'] = SITE_URL . "/index.php?app=notify&act=async&code={$code}";//异步通知接口
			$order['return_url'] = SITE_URL . "/index.php?app=notify&act=async&code={$code}";//同步通知接口

			$form_html = $pay->get_code($order);

			echo $form_html;

		}

	}


	//提現
	function cash()
	{   // 当前位置
		$this->_capital_position('提现');
		/* 当前用户中心菜单 */
		$this->_curitem('my_currency');

		/* 当前所处子菜单 */
		$this->_curmenu('my_currency');

		$user = $this->visitor->get();
		//用户资金
		$model_usermoney =& m('usermoney');
		$usermoney_info  = $model_usermoney->get_info_by_user_id($user['user_id']);
		$usermoney_info['user_money']  = number_format($usermoney_info['user_money'],2);
		$usermoney_info['frozen_money'] = number_format($usermoney_info['frozen_money'],2);

		$total_money = $usermoney_info['user_money']+$usermoney_info['frozen_money'];
		$total_money = number_format($total_money,2);

		$this->assign('user',$user);
		$this->assign('usermoney_info',$usermoney_info);
		$this->assign('total_money',$total_money);

		$model_certification =& m('usersafety');
		$cert_info = $model_certification->get("user_id='{$user['user_id']}'");

		$pay_password  = empty($cert_info['pay_password'])?0:1;//是否设置支付密码
		$renzheng      = empty($cert_info['is_certificate'])?0:1;//是否通过实名认证

		$this->assign('pay_password',$pay_password);
		$this->assign('renzheng',$renzheng);

		if($pay_password == 0 || $renzheng ==0)
		{
			$this->display('capital_cash.html');
		}

		else
		{

			$this->import_resource(array(
			'script' => array(

			array(
			'path' => 'jquery.plugins/jquery.validate.js',
			'attr' => '',
			),
			array(
			'path' => 'jquery.plugins/additional-methods.js',
			'attr' => '',
			),
			),

			));

			$model_userbank =& m('userbank');
			/*银行列表 */
			$bank_list = $model_userbank->findAll(array(
			'conditions'    => "user_id=" . $this->visitor->get('user_id'),
			'limit'         => '10',
			'order'         => 'is_default DESC',
			));

			//print_r($bank_list);

			$this->assign('bank_list',$bank_list);
			$this->display('capital_cash_bank.html');
		}

	}

	//提交表单
	function cash_submit()
	{
		$money  = floatval($_POST['cash_amount']);//金额
		if($money <= 0)
		{
			$this->show_warning('提现金额必须大于0元');
			exit;
		}
		if($money > 500)
		{
			$this->show_warning('最高每日提现总金额500元');
			die();
		}
		$bank_id  = intval(trim($_POST['sendType']));//选择了哪个银行

		if(empty($bank_id))
		{
			$this->show_warning('请选择银行！');
			die();
		}

		$params=base64_encode($money.','.$bank_id);
		//重新跳转，防止表单刷新
		header('Location:index.php?app=paycenter&act=need_pay_password&from='.$_GET['from'].'&params='.$params);
	}


	//提现需要输入支付密码
	function need_pay_password()
	{


		if(!IS_POST)
		{
			$this->import_resource(array(
			'script' => array(

			array(
			'path' => 'jquery.plugins/jquery.validate.js',
			'attr' => '',
			),
			array(
			'path' => 'jquery.plugins/additional-methods.js',
			'attr' => '',
			),
			),

			));

			// 当前位置
			$this->_capital_position('提现');

			/* 当前用户中心菜单 */
			$this->_curitem('my_currency');

			/* 当前所处子菜单 */
			$this->_curmenu('my_currency');

			$params = html_script(trim($_GET['params']));

			$user = $this->visitor->get();
			//用户资金
			$model_usermoney =& m('usermoney');
			$usermoney_info  = $model_usermoney->get("user_id = {$user['user_id']}");

			$total_money = $usermoney_info['user_money']+$usermoney_info['frozen_money'];//可用金额+冻结金额
			$usermoney_info['total_money'] = $total_money;//账户金额

			$this->assign('user',$user);
			$this->assign('usermoney_info',$usermoney_info);

			$arr     =  explode(',',base64_decode($params));
			$money   =  $arr[0];
			$bank_id  =  $arr[1];

			if(empty($money) || empty($bank_id))
			{
				$this->show_warning('校验错误');
				exit;
			}
			//用户银行信息
			$model_userbank =& m('userbank');
			$bank_info  = $model_userbank->get("bank_id = {$bank_id} and user_id = {$user['user_id']}");
			$bank_info['money'] = $money;

			$this->assign('bank_info', $bank_info);//显示页面中
			$this->display('capital_pay_password.html');
		}
		else
		{
			//重新跳转，防止表单刷新
			$pay_password = trim($_POST['pay_password']);
			$money   =  trim($_POST['amount']);
			$bank_id  = intval($_POST['bank_id']);
			$key   = '~!4c26e267d63fcd9abd19e0c8920989';
			$sign  = md5($pay_password.$key);
			$params=base64_encode($money.','.$bank_id.','.$pay_password.','.$sign);
			header('Location:index.php?app=paycenter&act=cash_confirm&from='.$_GET['from'].'&params='.$params);
		}

	}

	//提交表单
	function cash_confirm()
	{
		//表单提交后确认页
		// 当前位置
		$params = html_script(trim($_GET['params']));

		$arr     =  explode(',',base64_decode($params));
		$money   =  $arr[0];
		$bank_id  =  $arr[1];
		$pay_password    =  $arr[2];
		$sign     =  $arr[3];

		if(empty($money) || empty($bank_id) ||empty($pay_password) ||empty($sign))
		{
			$this->show_warning('参数错误');
			exit;
		}

		$API_SECURITY_TICKET = '~!4c26e267d63fcd9abd19e0c8920989';
		$ticket  = md5($pay_password.$API_SECURITY_TICKET);
		if($sign != $ticket)
		{
			$this->show_warning('效验码错误');
			die();
		}


		$user_id = intval($this->visitor->get('user_id'));
		//用户资金
		$model_usermoney =& m('usermoney');
		$usermoney_info  = $model_usermoney->get("user_id = {$user_id}");

		if($usermoney_info['user_money'] < $money)
		{
			$this->show_warning('可提取金额不能大于用户可用金额');
			die();
		}

		//用户提现,每天最多可提现3次，最高每日提现总金额500元
		$model_usercash =& m('usercash');
		$num = $model_usercash->get_count_by_day($user_id);
		if($num >= 3)
		{
			$this->show_warning('每天最多可提现3次');
			die();
		}

		$sum = $model_usercash->get_sum_by_day($user_id);
		if($sum > 500)
		{
			$this->show_warning('最高每日提现总金额500元');
			die();
		}

		//用户银行信息
		$model_userbank =& m('userbank');
		$bank_info  = $model_userbank->get("bank_id = {$bank_id} and user_id = {$user_id}");

		$data = array(
		'user_id'      => intval($this->visitor->get('user_id')),
		'cash_code'      => $this->genOrderCode(),//流水号
		'real_name' => $bank_info['real_name'],
		'bank_name' => $bank_info['bank_name'],
		'sub_branch'   => $bank_info['sub_branch'],
		'bank_account'  => $bank_info['bank_account'],
		'amount'  =>   $money,
		'add_time'   => time(),
		'cash_ip'   => real_ip(),
		);

		if (!($cash_id = $model_usercash->add($data)))
		{
			$this->show_message($model_usercash->get_error());
			exit;
		}
		else
		{
			//冻结资金
			$money_arr['user_money']   = $usermoney_info['user_money'] - $money; //减少可用金额
			$money_arr['frozen_money']   = $usermoney_info['frozen_money'] + $money ;//冻结金额
			$model_usermoney->edit("user_id = {$user_id}",$money_arr);
			header('Location:index.php?app=paycenter&act=cash_result&from='.$_GET['from']);
			//成功跳转，防止表单刷新
		}

	}
	//提现结果提示页
	function cash_result()
	{
		// 当前位置
		$this->_capital_position('提现');
		/* 当前用户中心菜单 */
		$this->_curitem('my_currency');

		/* 当前所处子菜单 */
		$this->_curmenu('my_currency');
		$this->display('capital_cash_confirm.html');
	}

	//提现记录
	function cash_log()
	{
		// 当前位置
		// 当前位置
		$this->_capital_position('提现记录');
		/* 当前用户中心菜单 */
		$this->_curitem('my_currency');

		/* 当前所处子菜单 */
		$this->_curmenu('my_currency');

		//用户提现记录
		$page = $this->_get_page();
		$model_cash =& m('usercash');

		$cash_list = $model_cash->findAll(array(
		'conditions'    => "user_id=" . $this->visitor->get('user_id'),
		'count'         => true,
		'limit'         => $page['limit'],
		'order'         => 'add_time DESC',
		));
		require(ROOT_PATH . '/includes/cfg_status.php');//配置文件
		foreach ($cash_list as $key=>$val)
		{
			$cash_list[$key]['status'] = $cfg_cash_status[$val['outstatus']];
			$cash_list[$key]['result'] = $val['admin_note'];
			$cash_list[$key]['bank']   = $cfg_bank_name[$val['bank_name']];
		}



		$page['item_count'] = $model_cash->getCount();
		$this->_format_page($page);


		$this->assign('cash_list', $cash_list);
		$this->assign('page_info', $page);
		$this->display('capital_cash_log.html');

	}


	function addbank()
	{
		if (!IS_POST)
		{

			// 当前位置
			$this->_capital_position('添加银行卡');


			/* 当前用户中心菜单 */
			$this->_curitem('my_currency');

			/* 当前所处子菜单 */
			$this->_curmenu('my_currency');

			$this->import_resource(array(
			'script' => array(

			array(
			'path' => 'jquery.plugins/jquery.validate.js',
			'attr' => '',
			),
			array(
			'path' => 'jquery.plugins/additional-methods.js',
			'attr' => '',
			),
			),

			));
			//用户验证
			$user = $this->visitor->get();

			$model_certification =& m('certification');
			$cert_info = $model_certification->get("user_id='{$user['user_id']}'");


			$user['real_name'] = $cert_info['certification_type']==1?$cert_info['real_name']:$cert_info['company_name'];
			//print_r($user);die();

			$this->assign('user',$user);

			$this->display('capital_cash_addbank.html');
		}
		else
		{

			if (empty($_POST['real_name']) || empty($_POST['bank_name']) || empty($_POST['bank_account']) )
			{

				$this->show_warning('数据不完整，请重新填写！');
				return;
			}
			$data = array(
			'user_id'      => intval($this->visitor->get('user_id')),
			'real_name' => html_script($_POST['real_name']),
			'bank_name' => html_script($_POST['bank_name']),
			'sub_branch'   => html_script($_POST['sub_branch']),
			'bank_account'  => html_script($_POST['bank_account']),
			'add_time'   => time(),
			);

			$model_userbank =& m('userbank');

			$sql = "SELECT COUNT(user_id) FROM {$model_userbank->table} WHERE `user_id` = " . $this->visitor->get('user_id');
			if((int)$model_userbank->getOne($sql)>=10)
			{
				$this->show_warning('最多只能添加10个银行卡！');
				return;
			}

			/* 检查名称是否已存在 */
			if (!$model_userbank->unique(trim($data['bank_account']),$data['user_id']))
			{
				$this->show_warning('你已添加过此银行卡');
				return;
			}

			if (!($bank_id = $model_userbank->add($data)))
			{
				$this->show_message($model_userbank->get_error());
				return;
			}
			else
			{
				$this->show_message('添加银行卡成功！','back_list', 'index.php?app=paycenter&act=cash&from='.$_GET['from']);

			}
		}


	}

	//ajax 设置银行为默认
	function ajaxSetBankCardDefault()
	{
		$bank_id = intval($_REQUEST['id']);
		if(empty($bank_id))
		{
			echo 'error';die();
		}
		$data = array(
		'is_default'  => 1,
		);

		$model_userbank =& m('userbank');
		$where = "bank_id = {$bank_id} AND user_id=" . $this->visitor->get('user_id');
		$bank_info      = $model_userbank->get($where);
		if($bank_info['is_default']==1)
		{
			echo "had";die();//已经是默认了
		}

		$model_userbank->edit("user_id=" . $this->visitor->get('user_id') ." AND is_default =1" , array('is_default'  => 0));//默认的设置为非默认
		$model_userbank->edit($where, $data);//设置为默认

		if ($model_userbank->has_error())
		{
			echo 'error';die();
		}
		else
		{
			echo "done";die();
		}
	}
	//ajax删除银行
	function ajaxDelBankCard()
	{
		$bank_id = intval($_REQUEST['id']);
		if(empty($bank_id))
		{
			echo 'error';die();
		}

		$model_userbank =& m('userbank');
		$where = "bank_id = {$bank_id} AND user_id=" . $this->visitor->get('user_id');
		$model_userbank->drop($where);

		if ($model_userbank->has_error())
		{
			echo 'error';die();
		}
		else
		{
			echo "done";die();
		}
	}
	//检查提取现金是不是不大于可用资金
	function ajaxCheckAmount()
	{
		$cash_amount = empty($_GET['cash_amount']) ? '' : trim($_GET['cash_amount']);
		$uid   =  intval($this->visitor->get('user_id'));

		if (!$cash_amount)
		{
			echo ecm_json_encode(false);
			die();
		}

		//用户资金
		$model_usermoney =& m('usermoney');
		$usermoney_info  = $model_usermoney->get("user_id={$uid}");

		if ($usermoney_info['user_money'] < $cash_amount)
		{
			echo ecm_json_encode(false);
			die();
		}
		else
		{
			echo ecm_json_encode(true);
			die();
		}

	}
	//检查银行卡号是否唯一
	function ajaxCheckBankAccount()
	{
		$bank_account = empty($_GET['bank_account']) ? '' : trim($_GET['bank_account']);
		$uid   =  intval($this->visitor->get('user_id'));
		$model_userbank =& m('userbank');

		if (!$bank_account)
		{
			echo ecm_json_encode(false);
			die();
		}
		if (!$model_userbank->unique($bank_account,$uid))
		{
			echo ecm_json_encode(false);
			die();
		}
		else
		{
			echo ecm_json_encode(true);
			die();
		}

	}


	//收支明细
	function my_account()
	{
		// 当前位置
		// 当前位置
		$this->_capital_position('我的礼品卡');

		/* 当前用户中心菜单 */
		$this->_curitem('my_detail');

		/* 当前所处子菜单 */
		$this->_curmenu('my_detail');

		$user = $this->visitor->get();
		//用户资金
		$model_usermoney =& m('usermoney');
		$usermoney_info  = $model_usermoney->get_info_by_user_id($user['user_id']);

		$total_money = $usermoney_info['user_money']+$usermoney_info['frozen_money'];
		$usermoney_info['user_money']  = number_format($usermoney_info['user_money'],2);
		$usermoney_info['frozen_money'] = number_format($usermoney_info['frozen_money'],2);
		$total_money = number_format($total_money,2);

		$this->assign('user',$user);
		$this->assign('usermoney_info',$usermoney_info);
		$this->assign('total_money',$total_money);

		$this->import_resource(array(
		'script' => array(
		array(
		'path' => 'dialog/dialog.js',
		'attr' => 'id="dialog_js"',
		),
		array(
		'path' => 'jquery.ui/jquery.ui.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.ui/i18n/' . i18n_code() . '.js',
		'attr' => '',
		),
		array(
		'path' => 'jquery.plugins/jquery.validate.js',
		'attr' => '',
		),
		),
		'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css',
		));
		$this->_get_trans();
		$this->display('my_account.html');

	}


	//生成订单号:年月日时分秒+5位随机数字
	function genOrderCode()
	{
		return date('YmdHis').$this->genCode(4);
		//return time().$this->genCode();
	}

	//private 生成指定长度的数字随机数
	function genCode($len=5)
	{
		$rnd = mt_rand(("1".sprintf("%0".($len-1)."d", 0))+0,("1".sprintf("%0".$len."d", 0))-1);
		$var = sprintf("%0".$len."d", $rnd);
		return $var;
	}


	/**
     *    获取交易列表
     */
	function _get_trans()
	{

		$conditions = '';

		$month = html_script(trim($_GET['month']));
		$today = intval($_GET['today']);
		$btime = html_script(trim($_GET['btime']));
		$etime = html_script(trim($_GET['etime']));
		$trans_code = html_script(trim($_GET['order_sn']));

		if(!empty($btime))//查询开始时间
		{
			$start_time = strtotime($btime);
			$conditions .= ' AND change_time >= '.$start_time;
		}
		if(!empty($etime))//查询结束时间
		{
			$end_time = strtotime($etime)+24*60*60;
			$conditions .= ' AND change_time <'.$end_time;
		}

		if(!empty($month) && $month!='all')//最近多少个月
		{
			$start_time = strtotime("- $month month");
			$conditions .= ' AND change_time >= '.$start_time;
		}
		if(!empty($today))//今天
		{
			$start_time = strtotime(date('Y-m-d'));
			$conditions .= ' AND change_time >= '.$start_time;
		}

		if(!empty($trans_code))
		{
			$conditions .= " AND order_no = '$trans_code'";
		}

		$type = intval($_GET['type']);
		if($type==1 or $type==0)//礼品卡激活记录
		{
			$conditions .= " AND change_type = 3 ";
		}
		else //交易记录
		{
			$conditions .= " AND change_type <> 3 ";
		}

		//echo $conditions;
		$page = $this->_get_page();
		$model_accountlog =& m('accountlog');


		/* 查找订单 */
		$trans = $model_accountlog->findAll(array(
		'conditions'    => "user_id=" . $this->visitor->get('user_id') . "{$conditions}",
		'count'         => true,
		'limit'         => $page['limit'],
		'order'         => 'change_time DESC',
		));
		require(ROOT_PATH . '/includes/cfg_status.php');//配置文件
		foreach ($trans as $key=>$val)
		{
			if($val['change_type'] == 0)//充值
			{
				/*$model_usertrans =& m('user_trans');
				$sql = "SELECT trans_payment FROM {$model_usertrans->table} WHERE trans_code = '{$val['order_no']}'";
				$payment = $model_usertrans->getOne($sql);*/
			}
			else if($val['change_type'] == 1)//提现
			{
				$payment = 'bank';//银行汇款/转帐
			}
			else if($val['change_type'] == 2)
			{
				$payment = 'account';//礼品卡支付
			}
			else if($val['change_type'] == 3)
			{
				$payment = 'gift_card';//礼品卡兑换
			}
			else if($val['change_type'] == 4)
			{
				$payment = 'returns_account';//余额退款
				$cfg_pay_type['returns_account']= '余额退款';
			}
			else
			{
				$payment = 'admin_pay';//会员账户变动
			}

			$trans[$key]['trans_payment_name'] = $cfg_pay_type[$payment];
		}

		$page['item_count'] = $model_accountlog->getCount();
		$this->_format_page($page);

		$this->assign('month', $month);
		$this->assign('today', $today);
		$this->assign('btime', $btime);
		$this->assign('etime', $etime);
		$this->assign('order_sn', $trans_code);
		$this->assign('type', $type);
		$this->assign('trans', $trans);
		$this->assign('page_info', $page);
	}
	//资金管理所在位置
	function _capital_position($name='')
	{

		$from  = trim($_GET['from']);
		$this->assign('member_from', $from);
		if($from == 'seller')
		{
			$this->_curlocal(LANG::get('seller_center'),    'index.php?app=seller',
			'资金管理', 'index.php?app=paycenter&act=account_info&from=seller',
			$name);
		}
		else
		{
			$this->_curlocal(LANG::get('buyer_center'),    'index.php?app=member',
			'资金管理', 'index.php?app=paycenter&act=account_info&from=buyer',
			$name);
		}
	}

}

?>